﻿using UnityEngine;
using System.Collections;

public class ControlCamera : MonoBehaviour
{
    // --------------------------------------------------------------------------------
    public float Speed = 10.0f;
    private float MoveH;
    private float MoveV;
    private float RotateX;
    private float RotateY;
    // --------------------------------------------------------------------------------
    // Use this for initialization
    void Start()
    {
    }
    // --------------------------------------------------------------------------------
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(1)) //鼠标右键按下时开始控制
        {
            Rotate();
        }
        else if (Input.GetMouseButton(2)) //鼠标中键控制拖动
        {
            Drag();
        }
        else
        {
            Move();
        }
    }

    // --------------------------------------------------------------------------------
    private void Move()
    {
        float w = Input.GetAxis("Mouse ScrollWheel") *  2.0f;
        Vector3 val = Vector3.forward * w;
        Vector3 trans = transform.rotation * val;
        transform.localPosition += trans;
    }
    // --------------------------------------------------------------------------------
    private void Rotate()
    {
        RotateX = Input.GetAxis("Mouse X") * 4;
        RotateY = Input.GetAxis("Mouse Y") * 4;

        transform.Rotate(Vector3.up * RotateX, Space.World);
        transform.Rotate(Vector3.right * -RotateY, Space.Self);
        transform.Rotate(Vector3.up * RotateX, Space.World);

    }
    // --------------------------------------------------------------------------------
    private void Drag()
    {
        var pos = Input.GetAxis("Mouse X") * transform.right + Input.GetAxis("Mouse Y") * transform.up;
        transform.localPosition -= pos;
    }

}
